package com.pllandxzx.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.UUID;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * @ClassName ChatController
 * @Description TODO
 * @Author 小鹿
 * @Date 2025/6/15 15:49
 */

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController
{
    private final ChatClient chatClient;
    @PostMapping(value = "/chat", produces = "text/event-stream;charset=utf-8")
    public Flux<ServerSentEvent<String>> chat(@RequestBody String prompt){
        return chatClient.prompt()
                .user(prompt)
                .advisors(
                        spec -> spec
                                .param(CHAT_MEMORY_CONVERSATION_ID_KEY, UUID.randomUUID())
                                .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY,10))
                .stream()
                .content()
                .map(data -> ServerSentEvent.builder(data).build());
    }
}